package com.kmist.xiakexing.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.kmist.xiakexing.R;
import com.kmist.xiakexing.interfaces.IUserCallback;
import com.kmist.xiakexing.main.base.BaseApplication;
import com.kmist.xiakexing.main.socket.SockPresenter;
import com.kmist.xiakexing.presenters.PlayerPresenter;
import com.kmist.xiakexing.presenters.UserPreserter;
import com.kmist.xiakexing.tools.Config;
import com.kmist.xiakexing.utils.UserBean;
import com.kmist.xiakexing.view.CreatePlayerDialog;

public class LoginApplication extends BaseApplication implements View.OnClickListener, IUserCallback {

    private static final String TAG = "Login";
    private EditText login_edit_account;
    private EditText login_edit_passwd;
    private Button login_btn_login;
    private Button login_btn_register;
    private UserPreserter userPreserter;
    private PlayerPresenter playerPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_login);

        login_edit_account = findViewById(R.id.login_edit_account);
        login_edit_passwd = findViewById(R.id.login_edit_pass);
        login_btn_login = findViewById(R.id.login_btn_login);
        login_btn_register = findViewById(R.id.login_btn_register);
        login_btn_login.setOnClickListener(this);
        login_btn_register.setOnClickListener(this);

        SockPresenter sockPresenter = SockPresenter.getInstance();
        sockPresenter.connect();

        userPreserter = UserPreserter.getsInstance();
        userPreserter.setCallback(this);

        playerPresenter = PlayerPresenter.getInstance();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_btn_login:
                Login();
                break;
            case R.id.login_btn_register:
                new CreatePlayerDialog(this);
                break;
        }
    }

    private void Login() {
        String account = login_edit_account.getText().toString();
        String passwd = login_edit_passwd.getText().toString();
        if(account.equals("") || passwd.equals("")){
            Config.Alarm("账号密码不为空！");
        }else{
            userPreserter.login(new UserBean("",account, passwd));
        }
    }

    @Override
    public void onLoginSuccess() {

        playerPresenter.getPlayer();
        playerPresenter.updateOnlineLastTime();
        Intent intent = new Intent(Config.context, MainApplication.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void onLoginError() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Config.Alarm("登陆失败！请检查账号或密码");
                login_edit_passwd.setText("");
            }
        });

    }

    @Override
    public void onRegisterSuccess() {

    }

    @Override
    public void onRegisterExistError() {

    }

    @Override
    public void onRegisterError() {

    }

    @Override
    public void onConnected() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Config.Alarm("服务器连接成功！");
            }
        });
    }

    @Override
    public void onNetworkError() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Config.Alarm("服务器连接失败, 请检查网络后重试!");
            }
        });
    }

}
